package com.zbkj.crmeb.chant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.exception.CrmebException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.utils.DateUtil;
import com.zbkj.crmeb.chant.model.ChannelMerchant;
import com.zbkj.crmeb.chant.model.ChannelMerchantRenewal;
import com.zbkj.crmeb.chant.dao.ChannelMerchantRenewalDao;
import com.zbkj.crmeb.chant.request.AddRenewalRequest;
import com.zbkj.crmeb.chant.response.ChannelMerchantRenewalResponse;
import com.zbkj.crmeb.chant.service.ChannelMerchantRenewalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.crmeb.chant.service.IChannelMerchantService;
import com.zbkj.crmeb.system.model.SystemAdmin;
import com.zbkj.crmeb.system.service.SystemAdminService;
import com.zbkj.crmeb.user.response.MemberRecordResponse;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商户续费记录表 服务实现类
 * </p>
 *
 * @author wuzp
 * @since 2022-08-03
 */
@Service
public class ChannelMerchantRenewalServiceImpl extends ServiceImpl<ChannelMerchantRenewalDao, ChannelMerchantRenewal> implements ChannelMerchantRenewalService {

    @Autowired
    private IChannelMerchantService channelMerchantService;
    @Autowired
    private SystemAdminService systemAdminService;

    @Override
    public PageInfo<ChannelMerchantRenewalResponse> getRenewalList(Integer merId, PageParamRequest pageParamRequest) {
        List<ChannelMerchantRenewalResponse> list = new ArrayList<>();
        Page<Object> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<ChannelMerchantRenewal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(merId != null){
            lambdaQueryWrapper.eq(ChannelMerchantRenewal::getMerId,merId);
        }
        lambdaQueryWrapper.orderByDesc(ChannelMerchantRenewal::getId);
        List<ChannelMerchantRenewal> merchantRenewalList = this.list(lambdaQueryWrapper);
        if(CollectionUtils.isEmpty(merchantRenewalList)){
            return CommonPage.copyPageInfo(page,list);
        }
        List<Integer> merIdList = merchantRenewalList.stream().map(ChannelMerchantRenewal::getMerId).distinct().collect(Collectors.toList());
        List<ChannelMerchant> channelMerchantList = channelMerchantService.list(Wrappers.<ChannelMerchant>lambdaQuery().in(ChannelMerchant::getId, merIdList)
                .select(ChannelMerchant::getId, ChannelMerchant::getChannelName));
        Map<Integer, String> merMap = channelMerchantList.stream().collect(Collectors.toMap(ChannelMerchant::getId, ChannelMerchant::getChannelName));
        List<Integer> OperationIdList = merchantRenewalList.stream().map(ChannelMerchantRenewal::getOperationId).distinct().collect(Collectors.toList());
        List<SystemAdmin> systemAdminList = systemAdminService.list(Wrappers.<SystemAdmin>lambdaQuery().in(SystemAdmin::getId, OperationIdList).select(SystemAdmin::getId, SystemAdmin::getRealName));
        Map<Integer, String> adminMap = systemAdminList.stream().collect(Collectors.toMap(SystemAdmin::getId, SystemAdmin::getRealName));
        for (ChannelMerchantRenewal merchantRenewal : merchantRenewalList) {
            ChannelMerchantRenewalResponse response = new ChannelMerchantRenewalResponse();
            BeanUtils.copyProperties(merchantRenewal,response);
            response.setMerName(merMap.get(response.getMerId()));
            response.setOperationName(adminMap.get(response.getOperationId()));
            list.add(response);
        }
        return CommonPage.copyPageInfo(page,list);
    }

    @Override
    public boolean addRenewal(AddRenewalRequest request) {
        if(request.getExpirationTime().before(new Date())){
            throw new CrmebException("到期时间不能小于当前时间");
        }
        ChannelMerchantRenewal renewal = new ChannelMerchantRenewal();
        //查询商户到期日期
        ChannelMerchantRenewal merchantRenewal = this.getOne(Wrappers.<ChannelMerchantRenewal>lambdaQuery().eq(ChannelMerchantRenewal::getMerId, request.getMerId()).orderByDesc(ChannelMerchantRenewal::getExpirationTime).last("LIMIT 1"));
        if(merchantRenewal != null){
            //比较日期
            if(merchantRenewal.getExpirationTime().before(new Date())){
                //过期
                renewal.setEffectTime(new Date());
            }else {
                if(merchantRenewal.getExpirationTime().after(request.getExpirationTime())){
                    throw new CrmebException("请核对本次到期时间,上次到期时间:"+DateFormatUtils.format(merchantRenewal.getExpirationTime(),"yyyy-MM-dd HH:mm:ss"));
                }
                renewal.setEffectTime(DateUtil.addSecond(merchantRenewal.getExpirationTime(),1));
            }
        }else {
            renewal.setEffectTime(new Date());
        }
        renewal.setExpirationTime(request.getExpirationTime());
        renewal.setPrice(request.getPrice());
        renewal.setMerId(request.getMerId());
        renewal.setOperationId(request.getFlag() ? 0 : systemAdminService.getAdminId());
        return this.save(renewal);
    }

}
